﻿using GameProtocol;
using GameProtocol.DTO;
using ServerApp.Biz;
using System;
using System.Collections.Generic;
using System.Linq;
using YServer;
using ServerApp.DAO;

public class LoginHandler:AbsOnceSendHandler, IMessageReceive
{
    public AccountBiz accBiz=BizFactory.accBiz;
    public RoleBiz roleBiz = BizFactory.roleBiz;

    public LoginHandler() {
        SetProType();
    }


    public void OnMessageReceive(YToken token,SocketModel model)
    {
        switch (model.command)
        {
            case LoginProtocol.Login_CREQ:
                LoginResquest(token, model);
                break;
            case LoginProtocol.Res_CREQ:
                ResResquest(token, model);
                break;
            case LoginProtocol.Logined_CREQ:
                Logined(token);
                break;
            case LoginProtocol.Role_create_CREQ:
                if (model.GetTransModel<string>()!=null&&model.GetTransModel<string>().Trim() != "")
                {
                    Role_Create(token, model);
                }
                break;
            default:
                break;
        }
    }

    void LoginResquest(YToken token, SocketModel model)
    {
        
        AccountDTO acc_dto = model.GetTransModel<AccountDTO>();
        if (acc_dto == null) return;

        //执行账号事务中的登录，并返回结果
        ThreadExcute.Insatnce.Excute(delegate {
            int result= accBiz.LoginBiz(token, acc_dto.name, acc_dto.passwd);

            Send(token, TypeProtocol.Login, 0, LoginProtocol.Login_SRES, result);
        });
    }


    void ResResquest(YToken token, SocketModel model)
    {
        AccountDTO acc_dto = model.GetTransModel<AccountDTO>();
        if (acc_dto == null) return;

        //执行账号事务中的注册，并返回结果
        ThreadExcute.Insatnce.Excute(delegate
        {
            int result = accBiz.ResBiz(token, acc_dto.name, acc_dto.passwd);

            Send(token, TypeProtocol.Login, 0, LoginProtocol.Res_SRES, result);
        });
    }

    void Logined(YToken token) {
        //Console.WriteLine("logined");
        //账号登陆成功，进行角色登陆
        ThreadExcute.Insatnce.Excute(
                delegate {
                    int result = roleBiz.RoleLogin(token);
                    //如果登陆成功
                    if (result == 0)
                    {
                        Role role = roleBiz.GetRoleByToken(token);
                        RoleDTO dto = new RoleDTO(role.accid, role.name, role.coin, role.level, role.exp, role.next_exp, role.win, role.lost);
                        Send(token, TypeProtocol.Login, result, LoginProtocol.Logined_SRES, dto);
                    }
                    else {//登录失败
                        Send(token, TypeProtocol.Login, result, LoginProtocol.Logined_SRES);
                    }

                }
            );
    }

    /// <summary>
    /// 创建角色
    /// </summary>
    /// <param name="token"></param>
    /// <param name="model"></param>
    void Role_Create(YToken token,SocketModel model) {
        ThreadExcute.Insatnce.Excute(
            delegate {
                int result = roleBiz.RoleCreateBiz(token, model.GetTransModel<string>());
                Send(token, TypeProtocol.Login,0, LoginProtocol.Role_create_SRES, result);
            });
    }




    public void OnClientConn(YToken token)
    {
        
    }

    public void OnClientClose(YToken token, string error)
    {
        ThreadExcute.Insatnce.Excute(delegate {
            roleBiz.CloseBiz(token);
            accBiz.CloseBiz(token);
        });
    }

    public override void SetProType()
    {
        type = TypeProtocol.Login;
    }
}
